#include "function_base_test.h"
#include "viml/compiler.h"
#include "viml/vm.h"
#include <gtest/gtest.h>

namespace viml {

/**
 * 函数调用内存测试
 */
class FunctionMemoryTest : public FunctionCallTestBase {
protected:
    void SetUp() override {
        FunctionCallTestBase::SetUp();
    }
};

// FC-401: 测试函数调用内存分配
TEST_F(FunctionMemoryTest, FunctionCallMemoryAllocation) {
    std::string source = R"(
function create_objects()
    let objects = []
    let i = 1
    while i <= 10
        let obj = {"id": i, "value": "object_" .. i}
        let objects = objects + [obj]
        let i = i + 1
    endwhile
    return objects
endfunction

let objects = create_objects()
echo len(objects)
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "FunctionCallMemoryAllocation");
    
    // 检查内存分配统计
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-402: 测试递归调用内存使用
TEST_F(FunctionMemoryTest, RecursiveCallMemoryUsage) {
    std::string source = R"(
function recursive_create_list(n)
    if n <= 0
        return []
    endif
    let rest = recursive_create_list(n - 1)
    return [n] + rest
endfunction

let list = recursive_create_list(10)
echo len(list)
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "RecursiveCallMemoryUsage");
    
    // 检查内存使用情况
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-403: 测试内存泄漏检测
TEST_F(FunctionMemoryTest, MemoryLeakDetection) {
    std::string source = R"(
function create_temp_objects()
    let temp_list = []
    let i = 1
    while i <= 10
        let temp_obj = {"temp": i, "data": "temporary_data_" .. i}
        let temp_list = temp_list + [temp_obj]
        let i = i + 1
    endwhile
    // 不返回任何内容，局部变量应该被正确释放
endfunction

create_temp_objects()
echo "Function completed"
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "MemoryLeakDetection");
    
    // 检查执行完成后的状态
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-404: 测试大对象参数传递内存使用
TEST_F(FunctionMemoryTest, LargeObjectParameterMemory) {
    std::string source = R"(
function process_large_dict(dict)
    let count = 0
    // 简单计数字典项数
    return len(dict)
endfunction

function create_large_dict()
    let dict = {}
    let i = 1
    while i <= 20
        let dict[i] = "value_" .. i
        let i = i + 1
    endwhile
    return dict
endfunction

let large_dict = create_large_dict()
let result = process_large_dict(large_dict)
echo result
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "LargeObjectParameterMemory");
    
    // 检查内存使用情况
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-405: 测试函数闭包内存使用
TEST_F(FunctionMemoryTest, ClosureMemoryUsage) {
    std::string source = R"(
function create_counter()
    let count = 0
    function counter()
        count = count + 1
        return count
    endfunction
    return counter
endfunction

let counter1 = create_counter()
let counter2 = create_counter()

let result1 = counter1()
let result2 = counter1()
let result3 = counter2()

echo result1
echo result2
echo result3
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "ClosureMemoryUsage");
    
    // 检查内存使用情况
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-406: 测试函数返回大对象内存使用
TEST_F(FunctionMemoryTest, LargeObjectReturnMemory) {
    std::string source = R"(
function create_large_list()
    let list = []
    let i = 1
    while i <= 30
        let list = list + [i * 2]
        let i = i + 1
    endwhile
    return list
endfunction

function sum_list(lst)
    let sum = 0
    for item in lst
        let sum = sum + item
    endfor
    return sum
endfunction

let large_list = create_large_list()
let result = sum_list(large_list)
echo result
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "LargeObjectReturnMemory");
    
    // 检查内存使用情况
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

// FC-407: 测试嵌套函数调用内存管理
TEST_F(FunctionMemoryTest, NestedFunctionCallMemory) {
    std::string source = R"(
function outer_func(x)
    function inner_func(y)
        function innermost_func(z)
            return z * 2
        endfunction
        return innermost_func(y) + 1
    endfunction
    return inner_func(x) + 1
endfunction

let result = outer_func(10)
echo result
)";
    
    // 重置统计信息
    vm.reset_statistics();
    
    // 执行测试
    compileAndExecute(source, "NestedFunctionCallMemory");
    
    // 检查内存使用情况
    const auto& stats = vm.get_statistics();
    EXPECT_GT(stats.instructions_executed, 0);
}

} // namespace viml